import Vue from "vue";
import Router from "vue-router";
import { setLocalStorage, getLocalStorage } from "@/util/local-storge.js";
import home from "./home.js";
import PageTransition from "@/components/PageTransition.vue";
import Index from "@/view/index/index.vue";

import Home from "@/view/home/home.vue";
import TaskHouse from "@/view/taskHouse/taskHouse.vue";
import PublishTask from "@/view/publishTask/publishTask.vue";
import Wallet from "@/view/mine/wallet.vue";
import Mine from "@/view/mine/mine.vue";
import Pay from "@/view/pay/pay.vue";

Vue.use(Router);

Router.prototype.goBack = function() {
    this.isBack = true;
    window.history.back();
    // window.history.go(-1)
};

Vue.use(Router);
const router = new Router({
    mode: "history",
    // base: '/dist',
    routes: [{
        path: "/",
        name: "PageTransition",
        component: PageTransition,
        meta: {
            keepAlive: false, // 需要被缓存
        },
        children: [{
                path: "",
                component: Index,
                redirect: "/home",
                meta: {
                    keepAlive: true, // 需要被缓存
                },
                children: [{
                        path: "/home",
                        component: Home,
                    },
                    {
                        path: "/taskHouse",
                        component: TaskHouse,
                    },
                    {
                        path: "/publishTask",
                        component: PublishTask,
                    },
                    {
                        path: "/wallet",
                        component: Wallet,
                    },
                    {
                        path: "/mine",
                        component: Mine,
                    },
                ],
            },
            {
                path: "/pay",
                component: Pay,
            },
            ...home,
        ],
    }, ],
    scrollBehavior() {
        return {
            x: 0,
            y: 0,
        };
    },
});
// router.beforeEach((to, from, next) => {
//   const { user_id } = getLocalStorage('user_id');
//   if (!user_id) {
//     if (to.meta.login) {
//       next({ name: 'login', query: { redirect: to.name } });
//       return;
//     }
//   }
//   const pageTitle = to.meta.title ? to.meta.title : '未来鑫城';
//   window.document.title = pageTitle;
//   next();
// });

router.beforeEach((to, from, next) => {
    console.log(from.path);
    console.log(to.path);
    if (from.path == "/taskDetail" && (to.path == "/" || to.path == "/index")) {
        to.meta.type = 1; // 让 首页 缓存，即不刷新
        next();
    } else if (from.path == "/step1" && (to.path == "/" || to.path == "/index")) {
        to.meta.type = 2; // 让 首页 缓存，即不刷新
        next();
    } else if (
        from.path == "/walletOut" &&
        (to.path == "/" || to.path == "/index")
    ) {
        to.meta.type = 3; // 让 首页 缓存，即不刷新
        next();
    } else if (
        from.path == "/recharge" &&
        (to.path == "/" || to.path == "/index")
    ) {
        to.meta.type = 3; // 让 首页 缓存，即不刷新
        next();
    } else if (
        from.path == "/publishList" &&
        (to.path == "/" || to.path == "/index")
    ) {
        to.meta.type = 4; // 让 首页 缓存，即不刷新
        next();
    } else if (
        from.path == "/jdCharge" &&
        (to.path == "/" || to.path == "/index")
    ) {
        to.meta.type = 4; // 让 首页 缓存，即不刷新
        next();
    } else if (
        from.path == "/fdCharge" &&
        (to.path == "/" || to.path == "/index")
    ) {
        to.meta.type = 4; // 让 首页 缓存，即不刷新
        next();
    } else if (
        from.path == "/article" &&
        (to.path == "/" || to.path == "/index")
    ) {
        to.meta.type = 4; // 让 首页 缓存，即不刷新
        next();
    } else {
        to.meta.type = 0;
        next();
    }

    const pageTitle = to.meta.title ? to.meta.title : " ";
    window.document.title = pageTitle;
    next();
});

// router.beforeEach((to, from, next) => {
//   //判断是否需要缓存

// })

export default router;

// RouterModel.beforeEach((to, from, next) => {
//   // const { user_id } = getLocalStorage('user_id');
//   // if (!user_id) {
//   //   if (to.meta.login) {
//   //     next({ name: 'login', query: { redirect: to.name } });
//   //     return;
//   //   }
//   // }
//   const pageTitle = to.meta.title ? to.meta.title : '未来鑫城';
//   window.document.title = pageTitle;
//   next();
// });

// export default RouterModel;